<?php

/**
 * 商品分类接口
 */
class CategoryController extends ControllerBase {

    /**
     * 获取商品分类列表 [GET]
     * 接口请求地址：/category/get_list/
     * @param int $parent_id 父级分类Id，
     * @param int $ishome 是否在首页获取分类列表
     * @example 如果拉取顶级分类，parent_id传0
     * @example 倒序排序
     * @example 返回结果：{"retcode":0,"retmsg":[
     *      {cat_id: 1, cat_name: "iPhone5", cat_image: "图片地址", cat_order: 1},
     *      {cat_id: 2, cat_name: "iPhone6", cat_image: "图片地址", cat_order: 2}
     * ]}
     */
    public function get_listAction() {
        global $config;
        $ishome = InputUtil::digitDefault($this->request->get("ishome"), 0);
        $parentId = InputUtil::digitDefault($this->request->get("parent_id"), 0);
        if (is_numeric($parentId)) {
            $where = $ishome > 0 ? 'AND cat_showinhome = 1' : '';
            $cat = Category::find([
                "cat_parent = $parentId $where", "columns" => "cat_id, cat_name, cat_image, cat_order",
                "order" => "cat_order DESC"
            ]);
            $cat = $cat->toArray();
            foreach ($cat as &$ca) {
                $ca['cat_image'] = $config->base->domainbase . 'uploads/banner/' . $ca['cat_image'];
            }
            $this->echoMsg(0, $cat);
        } else {
            $this->echoErrcode(ErrorCode::$ERR_PARAMS_ERROR);
        }
    }

}
